<?php

// +----------------------------------------------------------------------
// | 产品类别栏目
// +----------------------------------------------------------------------

namespace Admins\Controller;

use Common\Controller\AdminBase;

class TryCategoryController extends AdminBase {

    //栏目列表
    public function index() {
        if (IS_POST) {
            $Category = M('TryCategory');
            foreach ($_POST['sort'] as $id => $sort) {
                $Category->where(array('cateid' => $id))->save(array('sort' => $sort));
            }
            $this->success("排序更新成功！");
            exit;
        }
        $categorys = M('TryCategory')->select();
        $this->assign("categorys", $categorys);
        $this->display();
    }

    //添加栏目
    public function add() {
        if (IS_POST) {
            $Category = D("Admins/TryCategory");
            //批量添加
            $post = $_POST;
            $cateid = $Category->addCategory($post);
            if ($cateid) {
                $this->success("添加成功！", U("TryCategory/index"));
            } else {
                $error = $Category->getError();
                $this->error($error ? $error : '栏目添加失败！');
            }
            $this->success("添加栏目成功！", U("TryCategory/index"));
        }  else {
            //栏目列表 可以用缓存的方式
            $this->display();
        }
    }

    //编辑栏目
    public function edit() {
        if (IS_POST) {
            $catid = I("post.cateid", "", "intval");
            if (empty($catid)) {
                $this->error('请选择需要修改的栏目！');
            }
            $Category = D("Admins/TryCategory");
            $status = $Category->editCategory($_POST);
            if ($status) {
                $this->success("更新成功！", U("TryCategory/index"));
            } else {
                $error = $Category->getError(); //获取MODEL中错误
                $this->error($error ? $error : '栏目修改失败！');
            }
        } else {
            $cateid = I('get.cateid', 0, 'intval');
            $data = M('TryCategory')->where(array('cateid'=>$cateid))->find();
            $this->assign("data", $data);
            $this->display();
        }
    }



    //删除栏目
    public function delete() {
        $cateid = I("get.cateid", "", "intval");
        if (!$cateid) {
            $this->error("请指定需要删除的栏目！");
        }
        $Category=D("Admins/TryCategory");
        if (false == $Category->deleteCateid($cateid)) {
            $error = $Category->getError(); //获取MODEL中错误
            $this->error($error ? $error : '栏目删除失败！');
        }
        $this->success("栏目删除成功！", U("TryCategory/index"));
    }
}
